//
//  AccountViewController.m
//  JiFengDaRen
//
//  Created by zhuchao on 16/3/17.
//  Copyright © 2016年 SH_iOS. All rights reserved.
/*******
 *******我的粮仓界面
*********/
#import "AccountViewController.h"

#import "UserModel.h"
#import "SHWithdrawCashViewController.h"
#import "MyIncomeNoteViewController.h"
#import "RecordPageController.h"

@interface AccountViewController ()
@property(nonatomic,strong)UIImageView  *headerImageView;
@property(nonatomic,strong)UILabel *balanceNameLabel;//账户余额标题
@property(nonatomic,strong)UILabel *balanceLabel;//账户余额
@property(nonatomic,strong)UIView  *labelBgView;//标签背景
@property(nonatomic,strong)UIView  *lineVew;//分割
@property(nonatomic,strong)UILabel *timeLabel;//到账时间
@property(nonatomic,strong)UIButton *withdrawalBtn;//提现按钮


@end

@implementation AccountViewController
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;

    [self requstBalance];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatHeader];
    [self requstBalance];

}

#pragma mark - 设置状态栏字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return  UIStatusBarStyleLightContent;
}

#pragma mark -  创建头部
- (void)creatHeader{

    self.headerImageView = [[UIImageView alloc]init];
    self.headerImageView.image = [UIImage imageNamed:@"my_profit"];
    self.headerImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.headerImageView];
    
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
    }];
    
    UIView * topView = [UIView new];
    [self.headerImageView addSubview:topView];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.headerImageView);
        make.height.mas_offset(KNAVIGATIONBARHEIGHT);
    }];
    
    UILabel * titleLabel = [UILabel new];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"我的收益";
    titleLabel.font = KSystemFont(17);
    [topView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topView);
        make.top.equalTo(topView).offset(34);
    }];
    
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topView).offset(15);
        make.centerY.equalTo(titleLabel);
        make.width.mas_offset(30);
    }];
    
    UIButton * recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [recordBtn setTitle:@"记录" forState:UIControlStateNormal];
    [recordBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    recordBtn.titleLabel.font = KSHSystemFont(16);
    [recordBtn addTarget:self action:@selector(recordAction) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:recordBtn];
    
    [recordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topView).offset(-15);
        make.centerY.equalTo(titleLabel);
    }];
    
    self.balanceNameLabel = [[UILabel alloc]init];
    self.balanceNameLabel.text = @"收益余额 (元) ";
    self.balanceNameLabel.textAlignment = NSTextAlignmentCenter;
    self.balanceNameLabel.textColor = [UIColor whiteColor];
    self.balanceNameLabel.font = KSystemFont(14);
    [self.headerImageView addSubview:self.balanceNameLabel];
    
    [self.balanceNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView.mas_bottom).with.offset(__I6Height(22));
        make.centerX.mas_equalTo(self.headerImageView.mas_centerX);
        make.width.mas_equalTo(__I6Width(180));
        make.height.mas_equalTo(__I6Height(13));
    }];
    
    self.balanceLabel = [[UILabel alloc]init];
    self.balanceLabel.textAlignment = NSTextAlignmentCenter;
    self.balanceLabel.textColor = [UIColor whiteColor];
    self.balanceLabel.font = KSHSystemFont(33
                                           );
    [self.headerImageView addSubview:self.balanceLabel];
    
    [self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.balanceNameLabel.mas_bottom).offset(__I6Height(21));
        make.centerX.mas_equalTo(self.headerImageView.mas_centerX);
        make.width.mas_equalTo(KScreenWidth);
    }];
    

    self.withdrawalBtn = [[UIButton alloc]init];
    [self.withdrawalBtn setBackgroundImage:[UIImage imageNamed:@"btn_bg"] forState:UIControlStateNormal];
    [self.withdrawalBtn setTitle:@"提现" forState:UIControlStateNormal];
    self.withdrawalBtn.titleLabel.font = KSHSystemFont(15);
    [self.withdrawalBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    [self.withdrawalBtn addTarget:self action:@selector(withdrawCash:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.withdrawalBtn];
    
    [self.withdrawalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerImageView.mas_bottom).offset(__I6Height(70));
        make.centerX.mas_equalTo(self.view);
    }];

}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)recordAction{
    
    RecordPageController * pageVC = [[RecordPageController alloc] init];
    
    [self.navigationController pushViewController:pageVC animated:YES];
}

#pragma mark - 发起提现
- (void)withdrawCash:(UIButton *)sender{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hud hideAnimated:YES afterDelay:1.f];
    [SHUserCenterHttp finderSHUserBindCardStatusSuccess:^(int status) {
        SHWithdrawCashViewController * withdrawCashVc = [[SHWithdrawCashViewController alloc]init];
        withdrawCashVc.withdrawCashType = SHWithdrawCashMyWallte;
        [self.navigationController pushViewController:withdrawCashVc animated:YES];
    } complection:^(int status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            SHBindCardViewController *bindingBankCardVc = [[SHBindCardViewController  alloc]init];
            bindingBankCardVc.usingType = SHCashCardUsingTypeShAddCard;
            [self.navigationController pushViewController:bindingBankCardVc animated:YES];
        });

        
    } failure:^(int status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController pushViewController:[[SHRealNameViewController alloc]init] animated:YES];
        });

    }];
    
}

#pragma mark -- 获取余额数据
- (void)requstBalance{
    
    [MyDataService requestAFURL:KUSER_GET_CURRENCY httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            
            NSNumber *balance = result[@"data"][@"balance"];
            if ([balance intValue] == 0) {
                self.balanceLabel.text = @"暂无收益";
            }else{
                self.balanceLabel.text = [NSString stringWithFormat:@"%.2f",[balance floatValue]];
            }
            [[UserModel shareManager] setBalance:[balance floatValue]];
        }
        
    } failure:^(id erro) {
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
